﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;


namespace SMALLERP.QM
{
    public partial class QMSWX : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        public delegate void SJDDY(object sender, EventArgs e);
        public SJDDY sjddy;
        public QMSWX()
        {
            InitializeComponent();
        }

      
 

        private void button1_Click_1(object sender, EventArgs e)
        {
            string strSql = null;
            strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'select TB004 检验编号,RTRIM(QMSTB.UDF01) 检验项目,TB013 检验标准,TB020 正公差,TB021 负公差' select @sql1 = @sql1 + ' , max(case TC005 when ''' + TC005 + ''' then QMSTC.UDF51 else 0 end) [' + TC005 + ']' from (select DISTINCT TC005 from QMSTC WHERE TC001='" + textBox8.Text + "' AND TC002='" + textBox9.Text + "' AND TC003='" + textBox10.Text + "') as a set @sql1 = @sql1 + ' from QMSTB LEFT JOIN QMSTC ON TB001=TC001 AND TB002=TC002 AND TB003=TC003 AND TB004=TC004 AND TB014=TC008 WHERE TB001=''" + textBox8.Text + "'' AND TB002=''" + textBox9.Text + "'' AND TB003=''" + textBox10.Text + "'' AND TB021>0 AND TB014=''" + textBox15.Text + "'' group by TB004,RTRIM(QMSTB.UDF01),TB013,TB020,TB021 ORDER BY TB004' EXEC(@sql1)";
           //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
            try
            {
                DataTable dt;
                dataGridView1.Columns.Clear();
                dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }

            strSql = "select QMSTB.TB004 检验编号,QMSTB.TB013 AS 检验类别,QMSTB.UDF01 AS 检验项目,TB018 检验结果,TB012 备注 FROM QMSTB WHERE TB001='" + textBox8.Text + "' AND TB002='" + textBox9.Text + "' AND TB003='" + textBox10.Text + "' AND TB016='4' AND TB014='" + textBox15.Text + "'";
            //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
            try
            {
                DataTable dt;
                //dataGridView2.Columns.Clear();
                dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                dataGridView2.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }
     
           
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox11.Text = "0";
                textBox13.Text = "0";
                textBox14.Text = "0";
                textBox18.Text = "0";
                comboBox1.Text = "";
                string strSql = null;
                string djlx = "";
                djlx = textBox1.Text.ToString().Substring(0, 1);

                //if (djlx == "3")
                //{
                //    strSql = "SELECT CD004,MB002,MB003,INVMB.UDF01,CD008,CD001,CD002,CD003,CC003 FROM PURCD LEFT JOIN PURCC ON CD001=CC001 AND CD002=CC002 LEFT JOIN INVMB ON CD004=MB001 WHERE (CC006='" + textBox1.Text + "' AND CD003='0001') OR (CD001+'-'+CD002+'-'+CD003='" + textBox1.Text + "')";
                //}

                //if (djlx == "5")
                //{
                strSql = "SELECT CD004,MB002,MB003,INVMB.UDF01,CD008,CD001,CD002,CD003,CC003 FROM MOCCD LEFT JOIN MOCCC ON CD001=CC001 AND CD002=CC002 LEFT JOIN INVMB ON CD004=MB001 WHERE (CC006='" + textBox1.Text + "' AND CD003='0001') OR (CD001+'-'+CD002+'-'+CD003='" + textBox1.Text + "')";
                //}

                    try
                {
                    sdr1 = db.GetDataReader1(strSql);

                    while (sdr1.Read())
                    {
                        textBox3.Text = sdr1["CD004"].ToString();
                        textBox4.Text = sdr1["MB002"].ToString();
                        textBox5.Text = sdr1["MB003"].ToString();
                        textBox6.Text = sdr1["UDF01"].ToString();
                        textBox8.Text = sdr1["CD001"].ToString();
                        textBox9.Text = sdr1["CD002"].ToString();
                        textBox10.Text = sdr1["CD003"].ToString();
                        textBox16.Text = sdr1["CC003"].ToString();
                        textBox7.Text = Convert.ToDouble(sdr1["CD008"]).ToString();
                        textBox2.Text = "10";
                        textBox1.Text = "";
                    }

                    if (sdr1.HasRows == false)
                    {
                        MessageBox.Show("该单号不存在！", "软件提示");
                        //textBox1.Text = "";
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                    finally
                    {
                        sdr1.Close();
                    }

                    strSql = "SELECT ISNULL(right('000'+rtrim(cast(cast(MAX(TB014) as numeric(16))+1 as char(4))),4),'0001') JYXH FROM QMSTB WHERE TB001='" + textBox8.Text + "' AND TB002='" + textBox9.Text + "' AND TB003='" + textBox10.Text + "'";
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            //textBox15.Text = sdr1["JYXH"].ToString();
                            textBox15.Text = "0001";

                        }

                        if (sdr1.HasRows == false)
                        {
                            return;
                        }

                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                    finally
                    {
                        sdr1.Close();
                    }

                    strSql = "SELECT MG003 检验编号,UDF01 AS 检验项目,MG006 AS 检验标准,MG012 AS 正公差,MG013 AS 负公差 FROM QMSMG WHERE MG002='" + textBox3.Text + "' ORDER BY MG003";
                    //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                    try
                    {
                        DataTable dt;
                        dataGridView1.Columns.Clear();
                        dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                        dataGridView1.DataSource = dt;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }

                    for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                    {
                        DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                        string xh = "";
                        xh = "0" + i;
                        xh=xh.Substring(xh.Length-2,2);
                        acCode.Name = "YB" + xh;
                        acCode.DataPropertyName = "YB" + xh;
                        acCode.HeaderText = "样本" + xh;
                        dataGridView1.Columns.Add(acCode);
                    }

                    strSql = "SELECT  MG003 检验编号,MG006 AS 检验类别,UDF01 AS 检验项目,'' AS 检验结果,'' AS 备注 FROM QMSMG WHERE MG002='文本项目' ORDER BY MG003";
                    //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                    try
                    {
                        DataTable dt;
                        //dataGridView2.Columns.Clear();
                        dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                        dataGridView2.DataSource = dt;
                        
                       
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }

                    //for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                    //{
                    //    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                    //    acCode.Name = "YB" + i;
                    //    acCode.DataPropertyName = "YB" + i;
                    //    acCode.HeaderText = "样本" + i;
                    //    dataGridView2.Columns.Add(acCode);
                    //}
                }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strSql = null;
            strSql = "DELETE FROM QMSMG WHERE MG002='" + textBox3.Text + "'";

            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {
                   
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }
            //strSql = "DELETE FROM QMSMG WHERE MG002='文本项目'";

            //try
            //{
            //    if (db.ExecDataBySql1(strSql) > 0)
            //    {

            //    }
            //    else
            //    {

            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    return;
            //}

            string jybz = null;
            string jyxm=null;
            string jybh = null;
            double jysx = 0;
            double jyxx = 0;
            for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            {
                jybz = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验标准"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验标准"].Index].Value.ToString();
                jyxm = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验项目"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验项目"].Index].Value.ToString();
                jybh = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value.ToString();
         
                jysx = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["正公差"].Index].Value);
                jyxx = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["负公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["负公差"].Index].Value);

                if (jybh != "")
                {
                    strSql = "INSERT INTO QMSMG(COMPANY,CREATOR,CREATE_DATE,FLAG,MG001,MG002,MG003,MG006,MG008,MG012,MG013,UDF01) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','101','" + textBox3.Text + "','" + this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value.ToString() + "','" + jybz + "','****','" + jysx.ToString() + "','" + jyxx.ToString() + "','" + jyxm + "'";

                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                            //MessageBox.Show(jybh.ToString(), "软件提示");
                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                }
            }

            //for (int i = 0; i <= this.dataGridView2.Rows.Count - 1; i++)
            //{
            //    jybz = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYLB2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYLB2"].Index].Value.ToString();
            //    jyxm = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYXM2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYXM2"].Index].Value.ToString();
            //    jybh = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value.ToString();

            //    //jysx = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["正公差"].Index].Value);
            //    //jyxx = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["负公差"].Index].Value);

            //    if (jybh != "")
            //    {
            //        strSql = "INSERT INTO QMSMG(COMPANY,CREATOR,CREATE_DATE,FLAG,MG001,MG002,MG003,MG006,MG008,MG012,MG013,UDF01) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','101','文本项目','" + jybh + "','" + jybz + "','****','" + jysx.ToString() + "','" + jyxx.ToString() + "','" + jyxm + "'";

            //        try
            //        {
            //            if (db.ExecDataBySql1(strSql) > 0)
            //            {

            //            }
            //            else
            //            {

            //            }
            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.Message, "软件提示");
            //            return;
            //        }
            //    }
            //}
            MessageBox.Show("保存成功！", "软件提示");
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView1.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
      


        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {

                string jybh = (this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["检验编号"].Index].Value == null) ? "" : this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["检验编号"].Index].Value.ToString();

                if (jybh == "")
                {
                    this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["检验编号"].Index].Value = String.Format("{0}", 0);
                    this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["正公差"].Index].Value = "0";
                    this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["负公差"].Index].Value = "0";
                    this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["检验标准"].Index].Value = "0";
                    int bs = 0;
                    int jybs = 0;
                    for (int i = 0; i <= this.dataGridView1.RowCount - 1; i++)
                    {
                        jybs = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value == null) ? 0 : Convert.ToInt16(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value);

                        if (bs < jybs)
                        {
                            bs = jybs;
                        }

                    }


                    //for (int i = 0; i <= this.dataGridView2.RowCount - 1; i++)
                    //{
                    //    jybs = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value == null) ? 0 : Convert.ToInt16(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value);

                    //    if (bs < jybs)
                    //    {
                    //        bs = jybs;
                    //    }

                    //}
                    string xh = "";
                    xh = "0" + (bs+1);
                    xh = xh.Substring(xh.Length - 2, 2);
                    this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["检验编号"].Index].Value = xh.ToString();
                    for (int i = this.dataGridView1.Columns["负公差"].Index + 1; i <= this.dataGridView1.ColumnCount - 1; i++)
                    {
                        this.dataGridView1.CurrentRow.Cells[i].Value = "0";
                    }
                }
            }

            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "软件提示");
                //return;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("你确定要保存吗？", "提示信息", MessageBoxButtons.OKCancel);
            if (result == DialogResult.Cancel)
            {
                return;
            }     //窗体标题栏显示相关信息。
            else
            { 
            }

            string strSql = null;
            string djlx="";
            djlx=textBox8.Text.ToString().Substring(0,1);
            //strSql = "DELETE FROM QMSTA WHERE TA001='" + textBox8.Text + "' AND TA002='" + textBox9.Text + "' AND TA003='" + textBox10.Text + "' AND TAC04='" + textBox15.Text + "'";

            //try
            //{
            //    if (db.ExecDataBySql1(strSql) > 0)
            //    {

            //    }
            //    else
            //    {

            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    return;
            //}

            strSql = "DELETE FROM QMSTB WHERE TB001='" + textBox8.Text + "' AND TB002='" + textBox9.Text + "' AND TB003='" + textBox10.Text + "' AND TB014='" + textBox15.Text + "'";

            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {

                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }
            strSql = "DELETE FROM QMSTC WHERE TC001='" + textBox8.Text + "' AND TC002='" + textBox9.Text + "' AND TC003='" + textBox10.Text + "' AND TC008='" + textBox15.Text + "'";

            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {

                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }
            string jybz = null;
            string jyxm = null;
            string jybh = null;
            string jylx = null;
            string jyjg = null;
            double jysx = 0;
            double jyxx = 0;
            for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            {
                jybz = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验标准"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验标准"].Index].Value.ToString();
                jyxm = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验项目"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验项目"].Index].Value.ToString();
                jybh = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value.ToString();

                jysx = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["正公差"].Index].Value);
                jyxx = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["负公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["负公差"].Index].Value);

                if (jybh != "")
                {
                    strSql = "INSERT INTO QMSTB(COMPANY,CREATOR,CREATE_DATE,FLAG,TB001,TB002,TB003,TB004,TB013,TB014,TB020,TB021,TBC01,UDF01) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value.ToString() + "','" + jybz + "','" + textBox15.Text + "','" + jysx.ToString() + "','" + jyxx.ToString() + "','" + textBox15.Text + "','" + jyxm + "'";

                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {

                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                    double scza = 0.00;
                    string xh = "";
                    for (int j = this.dataGridView1.Columns["负公差"].Index + 1; j <= this.dataGridView1.Columns.Count-1; j++)
                    {

                        xh = "0" + (j - 4);
                        xh=xh.Substring(xh.Length - 2, 2);
                     scza = (this.dataGridView1.Rows[i].Cells[j].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells[j].Value);
                       
                
                        strSql = "INSERT INTO QMSTC(COMPANY,CREATOR,CREATE_DATE,FLAG,TC001,TC002,TC003,TC004,TC005,TC008,TCC01,UDF01,UDF51) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + jybh + "','样本" + xh + "','" + textBox15.Text + "','" + textBox15.Text +"',''," + scza + "";

                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {

                            }
                            else
                            {

                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            return;
                        }

                    }
                }
                
                
            }

            for (int i = 0; i <= this.dataGridView2.Rows.Count - 1; i++)
            {
                jybz = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYLB2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYLB2"].Index].Value.ToString();
                jyxm = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYXM2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYXM2"].Index].Value.ToString();
                jybh = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value.ToString();
                jyjg = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYJG2"].Index].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYJG2"].Index].Value.ToString();

                //jysx = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["正公差"].Index].Value);
                //jyxx = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["负公差"].Index].Value);

                if (jybh != "")
                {
                    strSql = "INSERT INTO QMSTB(COMPANY,CREATOR,CREATE_DATE,FLAG,TB001,TB002,TB003,TB004,TB013,TB014,TB016,TB018,TB020,TB021,TBC01,UDF01) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + jybh + "','" + jybz + "','" + textBox15.Text + "','4','" + jyjg + "','" + jysx.ToString() + "','" + jyxx.ToString() + "','" + textBox15.Text + "','" + jyxm + "'";

                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {

                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                    //string sczb = "";
                    //for (int j = this.dataGridView2.Columns["负公差"].Index + 1; j <= this.dataGridView2.Columns.Count - 1; j++)
                    //{


                    //    sczb = (this.dataGridView2.Rows[i].Cells[j].Value == null) ? "" : this.dataGridView2.Rows[i].Cells[j].Value.ToString();


                    //    strSql = "INSERT INTO QMSTC(COMPANY,CREATOR,CREATE_DATE,FLAG,TC001,TC002,TC003,TC004,TC005,TC008,TCC01,UDF01,UDF51) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + jybh + "','样本" + (j-4) + "','" + textBox15.Text + "','" + textBox15.Text +"','" + sczb + "',0";

                    //    try
                    //    {
                    //        if (db.ExecDataBySql1(strSql) > 0)
                    //        {

                    //        }
                    //        else
                    //        {

                    //        }
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        MessageBox.Show(ex.Message, "软件提示");
                    //        return;
                    //    }

                    //}
                }


            }

            if (comboBox1.Text == "允收" || comboBox1.Text == "特采" || comboBox1.Text == "合格" || comboBox1.Text == "拒收" || comboBox1.Text == "选别")
            {
                if (Convert.ToDouble(textBox7.Text) < (Convert.ToDouble(textBox11.Text) + Convert.ToDouble(textBox13.Text) + Convert.ToDouble(textBox14.Text)))
                {
                    MessageBox.Show("验收数+破坏数+报废数不能大于到货数，请核查！", "软件提示");
                    return;
                }

                strSql = "SELECT RTRIM(UDF01) AS JYJG FROM QMSTA WHERE TA001='" + textBox8.Text + "' AND TA002='" + textBox9.Text + "' AND TA003='" + textBox10.Text + "'";
                try
                {
                    sdr1 = db.GetDataReader1(strSql);

                    while (sdr1.Read())
                    {
                        jyjg = sdr1["JYJG"].ToString();
                    }

                    if (sdr1.HasRows == true)
                    {
                        MessageBox.Show("已经判定为" + jyjg + ",不允许重复判定！", "软件提示");
                        return;
                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                finally
                {
                    sdr1.Close();
                }
                double YTSL = Convert.ToDouble(textBox7.Text) - (Convert.ToDouble(textBox11.Text) - Convert.ToDouble(textBox13.Text) - Convert.ToDouble(textBox14.Text));
                strSql = "INSERT INTO QMSTA(COMPANY,CREATOR,CREATE_DATE,FLAG,TA001,TA002,TA003,TA004,TA005,TA006,TA007,TA008,TA009,TA010,TA011,TA012,TA013,TA014,TA015,TA016,TA017,TA018,TA019,TA020,TA021,TA022,TA023,TA024,TA025,TA026,TA027,TA028,TA029,TA030,TA031,TA032,TA033,TA034,TA035,TA036,TA037,TA038,TA039,TA040,TA041,TA042,TA043,TA044,TA045,TAC01,TAC02,TAC03,TAC04,UDF01) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "',CONVERT(CHAR(8),GETDATE(),112),'" + textBox16.Text + "','" + PropertyClass.OperatorCode + "'," + textBox13.Text + "," + YTSL.ToString() + "," + textBox12.Text + ",'',";
                strSql = strSql + "'2','2',0,'Y','" + textBox19.Text + "','','','',CONVERT(CHAR(8),GETDATE(),112),'" + PropertyClass.OperatorCode + "','N'," + textBox2.Text + ",0,0,0,'0001','到货验收单PURCAI02',0,'" + textBox16.Text + "','" + textBox17.Text + "',0,0,0,'N','','','','','',NULL,0,0,0,0,0," + textBox11.Text + ",0,0,'0001','" + comboBox1.Text + "'";
                //textBox18.Text = strSql;
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {

                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                if (djlx == "5")
                {

                    strSql = "INSERT INTO QMSTD(COMPANY,CREATOR,CREATE_DATE,FLAG,TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013,TD014,TD015,TD016,TD017,TD018,TD019,TD020,TD021,TD022,TD023,TD024,TD025,TD026,TD027,TD028,TD029,TD030,TD031,TD032,TD033,TD034,TD035,TD036,TD037,TD038,TD039,TD040,TD041,TD042,TD043,TD044,TD045,TDC01,TDC02,TDC03,UDF01) SELECT 'ZJHS','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "',CONVERT(CHAR(8),GETDATE(),112),'" + textBox16.Text + "','" + PropertyClass.OperatorCode + "'," + textBox13.Text + "," + YTSL.ToString() + "," + textBox12.Text + ",'',";
                    strSql = strSql + "'2','2',0,'Y','" + textBox19.Text + "','','','',CONVERT(CHAR(8),GETDATE(),112),'" + PropertyClass.OperatorCode + "','N'," + textBox2.Text + ",0,0,0,'0001','到货验收单PURCAI02',0,'" + textBox16.Text + "','" + textBox17.Text + "',0,0,0,'N','','','','','',NULL,0,0,0,0,0,'0001'," + textBox11.Text + ",0,'" + comboBox1.Text + "'";
                    //textBox18.Text = strSql;
                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {

                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }

                }

                    string jhph = "";
                    if (djlx == "3")
                    {
                        strSql = "SELECT RTRIM(CC005)+SUBSTRING(CONVERT(CHAR(8),GETDATE(),112),3,6) AS JYPH FROM PURCD LEFT JOIN PURCC ON CD001=CC001 AND CD002=CC002 WHERE CD001='" + textBox8.Text + "' AND CD002='" + textBox9.Text + "' AND CD003='" + textBox10.Text + "'";
                    }
                    if (djlx == "5")
                    {
                        strSql = "SELECT RTRIM(CC005)+SUBSTRING(CONVERT(CHAR(8),GETDATE(),112),3,6) AS JYPH FROM MOCCD LEFT JOIN MOCCC ON CD001=CC001 AND CD002=CC002 WHERE CD001='" + textBox8.Text + "' AND CD002='" + textBox9.Text + "' AND CD003='" + textBox10.Text + "'";
                    }
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            jhph = sdr1["JYPH"].ToString();
                        }

                        if (sdr1.HasRows == true)
                        {

                        }


                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                    finally
                    {
                        sdr1.Close();
                    }
                
                strSql = "INSERT INTO INVME(FLAG,ME001,ME002,ME003,ME004,ME005,ME006,ME007,ME008,ME009,ME010,ME011) SELECT '1','" + textBox3.Text + "','" + jhph + "','','','" + textBox8.Text + "','" + textBox9.Text + "','N','','','','' WHERE NOT EXISTS(SELECT * FROM INVME WHERE ME001='" + textBox3.Text + "' AND ME002='" + jhph + "')";

                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {

                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }

                if (djlx == "3")
                {
                    strSql = "UPDATE PURCD SET CD018=" + textBox11.Text + ",CD019=CD008-" + textBox11.Text + "-" + textBox14.Text + "-" + textBox13.Text + ",CD026=" + textBox14.Text + ",CD028=" + textBox13.Text + " WHERE CD001='" + textBox8.Text + "' AND CD002='" + textBox9.Text + "' AND CD003='" + textBox10.Text + "'";
                }
                if (djlx == "5")
                {
                    strSql = "UPDATE MOCCD SET CD014=" + textBox11.Text + ",CD015=CD008-" + textBox11.Text + "-" + textBox14.Text + "-" + textBox13.Text + ",CD022=" + textBox14.Text + ",CD024=" + textBox13.Text + " WHERE CD001='" + textBox8.Text + "' AND CD002='" + textBox9.Text + "' AND CD003='" + textBox10.Text + "'";
                }

                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {

                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }

            }

            MessageBox.Show("保存成功！", "软件提示");


        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "特采")
            {
                textBox12.Enabled = true;
            }
            else
            {
                textBox12.Enabled =false;
            }

            if (comboBox1.Text == "允收" || comboBox1.Text == "特采" || comboBox1.Text == "合格" || comboBox1.Text == "选别")
            {
                textBox11.Text = textBox7.Text;
            }
            else
            {
                textBox11.Text = "0";
            }
        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void QMSJD_Load(object sender, EventArgs e)
        {
            textBox11.Text = "0";
            textBox12.Text = "0";
            textBox13.Text = "0";
            textBox14.Text = "0";
            string yf = "0" + System.DateTime.Today.Month.ToString();
            yf = yf.Substring(yf.Length - 2, 2);
            string rq = "0" + System.DateTime.Today.Day.ToString();
            rq = rq.Substring(rq.Length - 2, 2);
            textBox16.Text = System.DateTime.Today.Year.ToString() + yf + rq;
            comboBox1.Text = "";
            textBox12.Enabled = false;
            if (JYCX.LX == "IQC")
            {
                textBox1.Text = JYCX.DB + "-" + JYCX.DH + "-" + JYCX.XH;
                //textBox1_KeyDown(null,null);
                //textBox1.Text += "textBox1_KeyDown" + System.Environment.NewLine;
                textBox8.Text = JYCX.DB;
                textBox9.Text = JYCX.DH;
                textBox10.Text = JYCX.XH;
                textBox15.Text = JYCX.CS;
                button1.PerformClick();
            }

        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            string strSql = null;
            if (e.KeyCode == Keys.Enter)
            {
                strSql = "SELECT MG003 检验编号,UDF01 AS 检验项目,MG006 AS 检验标准,MG012 AS 正公差,MG013 AS 负公差 FROM QMSMG WHERE MG002='" + textBox3.Text + "' ORDER BY MG003";
                //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                try
                {
                    DataTable dt;
                    dataGridView1.Columns.Clear();
                    dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                    dataGridView1.DataSource = dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                {
                    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                    string xh = "";
                    xh = "0" + i;
                    xh=xh.Substring(xh.Length-2,2);
                    acCode.Name = "YB" + xh;
                    acCode.DataPropertyName = "YB" + xh;
                    acCode.HeaderText = "样本" + xh;
                    dataGridView1.Columns.Add(acCode);
                }

                strSql = "SELECT MG003 检验编号,UDF01 AS 检验项目,MG006 AS 检验标准,MG012 AS 正公差,MG013 AS 负公差 FROM QMSMG WHERE MG002='文本项目' ORDER BY MG003";
                //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                try
                {
                    DataTable dt;
                    dataGridView2.Columns.Clear();
                    dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                    dataGridView2.DataSource = dt;
                   

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                //for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                //{
                //    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                //    acCode.Name = "YB" + i;
                //    acCode.DataPropertyName = "YB" + i;
                //    acCode.HeaderText = "样本" + i;
                //    dataGridView2.Columns.Add(acCode);
                //}

            }
        }

        private void textBox10_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox11.Text = "0";
                textBox13.Text = "0";
                textBox14.Text = "0";
                textBox18.Text = "0";
                comboBox1.Text = "";
                string strSql = null;
                strSql = "SELECT CD004,MB002,MB003,INVMB.UDF01,CD008,CD001,CD002,CD003,CC003 FROM PURCD LEFT JOIN PURCC ON CD001=CC001 AND CD002=CC002 LEFT JOIN INVMB ON CD004=MB001 WHERE CD001='" + textBox8.Text + "' AND CD002='" + textBox9.Text + "' AND CD003='" + textBox10.Text + "'";
                try
                {
                    sdr1 = db.GetDataReader1(strSql);

                    while (sdr1.Read())
                    {
                        textBox3.Text = sdr1["CD004"].ToString();
                        textBox4.Text = sdr1["MB002"].ToString();
                        textBox5.Text = sdr1["MB003"].ToString();
                        textBox6.Text = sdr1["UDF01"].ToString();
                        textBox16.Text = sdr1["CC003"].ToString();
                        textBox7.Text = Convert.ToDouble(sdr1["CD008"]).ToString();
                        textBox2.Text = "10";
                    }

                    if (sdr1.HasRows == false)
                    {
                        MessageBox.Show("该单号不存在！", "软件提示");

                        return;
                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                finally
                {
                    sdr1.Close();
                }

                strSql = "SELECT ISNULL(right('000'+rtrim(cast(cast(MAX(TB014) as numeric(16))+1 as char(4))),4),'0001') JYXH FROM QMSTB WHERE TB001='" + textBox8.Text + "' AND TB002='" + textBox9.Text + "' AND TB003='" + textBox10.Text + "'";
                try
                {
                    sdr1 = db.GetDataReader1(strSql);

                    while (sdr1.Read())
                    {
                        //textBox15.Text = sdr1["JYXH"].ToString();
                        textBox15.Text = "0001";

                    }

                    if (sdr1.HasRows == false)
                    {
                        return;
                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
                finally
                {
                    sdr1.Close();
                }

                strSql = "SELECT MG003 检验编号,UDF01 AS 检验项目,MG006 AS 检验标准,MG012 AS 正公差,MG013 AS 负公差 FROM QMSMG WHERE MG002='" + textBox3.Text + "' ORDER BY MG003";
                //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                try
                {
                    DataTable dt;
                    dataGridView1.Columns.Clear();
                    dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                    dataGridView1.DataSource = dt;
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message, "软件提示");
                    //return;
                }

                for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                {
                    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                    string xh = "";
                    xh = "0" + i;
                    xh=xh.Substring(xh.Length - 2, 2);
                    acCode.Name = "YB" + xh;
                    acCode.DataPropertyName = "YB" + xh;
                    acCode.HeaderText = "样本" + xh;
                    dataGridView1.Columns.Add(acCode);
                }

                strSql = "SELECT  MG003 检验编号,MG006 AS 检验类别,UDF01 AS 检验项目,'' AS 检验结果,'' AS 备注 FROM QMSMG WHERE MG002='文本项目' ORDER BY MG003";
                //MessageBox.Show(dataGridView1.Columns.Count.ToString(), "软件提示");
                try
                {
                    DataTable dt;
                    //dataGridView2.Columns.Clear();
                    dt = db.GetDataSet1(strSql, "QMSMG").Tables["QMSMG"];
                    dataGridView2.DataSource = dt;
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message, "软件提示");
                    //return;
                }

                //for (int i = 1; i <= Convert.ToInt16(textBox2.Text); i++)
                //{
                //    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                //    acCode.Name = "YB" + i;
                //    acCode.DataPropertyName = "YB" + i;
                //    acCode.HeaderText = "样本" + i;
                //    dataGridView2.Columns.Add(acCode);
                //}
                
                
            }

        }

        private void dataGridView2_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string jybh = (this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["JYBH2"].Index].Value == null) ? "" : this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["JYBH2"].Index].Value.ToString();

                if (e.ColumnIndex == this.dataGridView2.Columns["JYXM2"].Index && jybh == "")
                {
                    this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["JYBH2"].Index].Value = String.Format("{0}", 0);
                    //this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["正公差"].Index].Value = "0";
                    //this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["负公差"].Index].Value = "0";
                    int bs = 0;
                    int jybs = 0;
                    //for (int i = 0; i <= this.dataGridView1.RowCount - 1; i++)
                    //{
                    //    jybs = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value == null) ? 0 : Convert.ToInt16(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["检验编号"].Index].Value);

                    //    if (bs < jybs)
                    //    {
                    //        bs = jybs;
                    //    }

                    //}


                    for (int i = 0; i <= this.dataGridView2.RowCount - 1; i++)
                    {
                        jybs = (this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value == null) ? 0 : Convert.ToInt16(this.dataGridView2.Rows[i].Cells[this.dataGridView2.Columns["JYBH2"].Index].Value);

                        if (bs < jybs)
                        {
                            bs = jybs;
                        }

                    }

                    this.dataGridView2.CurrentRow.Cells[this.dataGridView2.Columns["JYBH2"].Index].Value = String.Format("{0}", bs + 1);

                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "软件提示");
                //return;
            }

            
        }

        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
           
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView2.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView2.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView2.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
      
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {

                if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["负公差"].Index)
                {
                    double sxz = 0.00;
                    double xxz = 0.00;
                    double bbz = 0.00;
                    sxz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["正公差"].Index].Value);
                    xxz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["负公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["负公差"].Index].Value);
                    bbz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["检验标准"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["检验标准"].Index].Value);

                    double scz = (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);

                    if (scz != 0 && (scz > (bbz + sxz+0.000001) || scz < (bbz - xxz-0.000001)))
                    {
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow;
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "软件提示");
                //return;
            }

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int i = this.dataGridView1.CurrentRow.Index;
            this.dataGridView1.Rows.RemoveAt(i);
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex >= 0)
                    {
                        //若行已是选中状态就不再进行设置
                        if (dataGridView1.Rows[e.RowIndex].Selected == false)
                        {
                            dataGridView1.ClearSelection();
                            dataGridView1.Rows[e.RowIndex].Selected = true;
                        }
                        //只选中一行时设置活动单元格
                        if (dataGridView1.SelectedRows.Count == 1)
                        {
                            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        }
                        //弹出操作菜单
                        contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                    }
                }
            }
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["负公差"].Index)
                {
                    double sxz = 0.00;
                    double xxz = 0.00;
                    double bbz = 0.00;
                    sxz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["正公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["正公差"].Index].Value);
                    xxz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["负公差"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["负公差"].Index].Value);
                    bbz = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["检验标准"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["检验标准"].Index].Value);

                    double scz = (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);

                    if (scz != 0 && (scz > (bbz + sxz+0.000001) || scz < (bbz - xxz-0.000001)))
                    {
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow;
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
                    }

                    if (scz != 0 && scz <= (bbz + sxz) && scz >= (bbz - xxz))
                    {
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Black;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
                //this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                return;
            }
        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }

        private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            int i = this.dataGridView2.CurrentRow.Index;
            this.dataGridView2.Rows.RemoveAt(i);
        }

        private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex >= 0)
                    {
                        //若行已是选中状态就不再进行设置
                        if (dataGridView2.Rows[e.RowIndex].Selected == false)
                        {
                            dataGridView2.ClearSelection();
                            dataGridView2.Rows[e.RowIndex].Selected = true;
                        }
                        //只选中一行时设置活动单元格
                        if (dataGridView2.SelectedRows.Count == 1)
                        {
                            dataGridView2.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        }
                        //弹出操作菜单
                        contextMenuStrip2.Show(MousePosition.X, MousePosition.Y);
                    }
                }
            }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

     
     }
  }
